home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevcp50.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  7.5 KB  |  225 lines

  1. /* Copyright (C) 1991, 1994, 1996, 1998 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevcp50.c,v 1.2 2000/09/19 19:00:12 lpd Exp $*/
  20. /* Mitsubishi CP50 color printer driver */
  21. #include "gdevprn.h"
  22. #define ppdev ((gx_device_printer *)pdev)
  23.  
  24. /***
  25.  *** Note: this driver was contributed by a user.  Please contact
  26.  ***       Michael Hu (michael@ximage.com) if you have questions.
  27.  ***/
  28.  
  29. /* The value of X_PIXEL and Y_PIXEL is gained by experiment */
  30. #define X_PIXEL   474
  31. #define Y_PIXEL   800
  32.  
  33. /* The value of FIRST_LINE and LAST_LINE is gained by experiment */
  34. /* Note: LAST-LINE - FIRST_LINE + 1 should close to Y_PIXEL */
  35. #define FIRST_LINE 140
  36. #define LAST_LINE  933
  37.  
  38. /* The value of FIRST is gained by experiment */
  39. /* There are 60 pixel(RGB) in the right clipped margin */
  40. #define FIRST_COLUMN    180
  41.  
  42. /* The value of X_DPI and Y_DPI is gained by experiment */
  43. #define X_DPI 154        /* pixels per inch */ 
  44. #define Y_DPI 187        /* pixels per inch */
  45.  
  46. /* The device descriptor */
  47. private dev_proc_print_page(cp50_print_page);
  48. private dev_proc_output_page(cp50_output_page);
  49.  
  50. private dev_proc_map_rgb_color(cp50_rgb_color);
  51. private dev_proc_map_color_rgb(cp50_color_rgb);
  52.  
  53. private gx_device_procs cp50_procs =
  54.   prn_color_procs(gdev_prn_open, cp50_output_page, gdev_prn_close,
  55.     cp50_rgb_color, cp50_color_rgb);
  56.  
  57. gx_device_printer far_data gs_cp50_device =
  58.   prn_device(cp50_procs, "cp50",
  59.     39,                /* width_10ths, 100mm */
  60.     59,                /* height_10ths,150mm  */
  61.     X_DPI, Y_DPI,
  62.     0.39, 0.91, 0.43, 0.75,        /* margins */
  63.     24, cp50_print_page);
  64.  
  65. int copies;
  66.  
  67. /* ------ Internal routines ------ */
  68.  
  69.  
  70. /* Send the page to the printer. */
  71. private int
  72. cp50_print_page(gx_device_printer *pdev, FILE *prn_stream)
  73. {    
  74.     int line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
  75.     byte *out = (byte *)gs_malloc(line_size, 1, "cp50_print_page(out)");
  76.     byte *r_plane = (byte *)gs_malloc(X_PIXEL*Y_PIXEL, 1, "cp50_print_page(r_plane)");
  77.     byte *g_plane = (byte *)gs_malloc(X_PIXEL*Y_PIXEL, 1, "cp50_print_page(g_plane)");
  78.     byte *b_plane = (byte *)gs_malloc(X_PIXEL*Y_PIXEL, 1, "cp50_print_page(b_plane)");
  79.     byte *t_plane = (byte *)gs_malloc(X_PIXEL*Y_PIXEL, 1, "cp50_print_page(t_plane)");
  80.     int lnum = FIRST_LINE;
  81.     int last = LAST_LINE;
  82.     int lines = X_PIXEL;
  83.     byte hi_lines, lo_lines;
  84.     byte num_copies;
  85.     int i,j;
  86.  
  87.  
  88. /*fprintf(prn_stream, "%d,%d,%d,", pdev->width, pdev->height, line_size);*/
  89.  
  90.     /* Check allocations */
  91.     if ( out == 0 || r_plane == 0 || g_plane == 0 || b_plane == 0 || 
  92.          t_plane == 0)
  93.     {    if ( out )
  94.             gs_free((char *)out, line_size, 1,
  95.                 "cp50_print_page(out)");
  96.         if (r_plane)
  97.             gs_free((char *)r_plane, X_PIXEL*Y_PIXEL, 1,
  98.                 "cp50_print_page(r_plane)");
  99.         if (g_plane)  
  100.             gs_free((char *)g_plane, X_PIXEL*Y_PIXEL, 1, 
  101.                 "cp50_print_page(g_plane)");
  102.         if (b_plane)  
  103.             gs_free((char *)b_plane, X_PIXEL*Y_PIXEL, 1, 
  104.                 "cp50_print_page(b_plane)");
  105.         if (t_plane)
  106.             gs_free((char *)t_plane, X_PIXEL*Y_PIXEL, 1, 
  107.                 "cp50_print_page(t_plane)");
  108.         return -1;
  109.     }
  110.  
  111.     /* set each plane as white */
  112.     memset(r_plane, -1, X_PIXEL*Y_PIXEL);
  113.     memset(g_plane, -1, X_PIXEL*Y_PIXEL);
  114.     memset(b_plane, -1, X_PIXEL*Y_PIXEL);
  115.     memset(t_plane, -1, X_PIXEL*Y_PIXEL);
  116.  
  117.     /* Initialize the printer */ /* see programmer manual for CP50 */
  118.     fprintf(prn_stream,"\033\101");
  119.     fprintf(prn_stream,"\033\106\010\001");
  120.     fprintf(prn_stream,"\033\106\010\003");
  121.  
  122.     /* set number of copies */
  123.     fprintf(prn_stream,"\033\116");
  124.     num_copies = copies & 0xFF;
  125.     fwrite(&num_copies, sizeof(char), 1, prn_stream);
  126.  
  127.     /* download image */
  128.     hi_lines = lines >> 8;
  129.     lo_lines = lines & 0xFF;
  130.  
  131.     fprintf(prn_stream,"\033\123\062");
  132.     fwrite(&hi_lines, sizeof(char), 1, prn_stream);
  133.     fwrite(&lo_lines, sizeof(char), 1, prn_stream);
  134.     fprintf(prn_stream,"\001"); /* dummy */
  135.  
  136.     /* Print lines of graphics */
  137.     while ( lnum <= last )
  138.        {
  139.         int i, col;
  140.         gdev_prn_copy_scan_lines(pdev, lnum, (byte *)out, line_size);
  141.         /*fwrite(out, sizeof(char), line_size, prn_stream);*/
  142.         for(i=0; i<X_PIXEL; i++)
  143.         {
  144.           col = (lnum-FIRST_LINE) * X_PIXEL + i;
  145.           r_plane[col] = out[i*3+FIRST_COLUMN];
  146.           g_plane[col] = out[i*3+1+FIRST_COLUMN];
  147.           b_plane[col] = out[i*3+2+FIRST_COLUMN];
  148.         }
  149.         lnum ++;
  150.        }
  151.  
  152.     /* rotate each plane and download it */
  153.     for(i=0;i<X_PIXEL;i++)
  154.       for(j=Y_PIXEL-1;j>=0;j--)
  155.         t_plane[(Y_PIXEL-1-j)+i*Y_PIXEL] = r_plane[i+j*X_PIXEL];
  156.     fwrite(t_plane, sizeof(char), X_PIXEL*Y_PIXEL, prn_stream);
  157.  
  158.     for(i=0;i<X_PIXEL;i++)
  159.       for(j=Y_PIXEL-1;j>=0;j--)
  160.         t_plane[(Y_PIXEL-1-j)+i*Y_PIXEL] = g_plane[i+j*X_PIXEL]; 
  161.     fwrite(t_plane, sizeof(char), X_PIXEL*Y_PIXEL, prn_stream);
  162.  
  163.     for(i=0;i<X_PIXEL;i++)
  164.       for(j=Y_PIXEL-1;j>=0;j--)
  165.         t_plane[(Y_PIXEL-1-j)+i*Y_PIXEL] = b_plane[i+j*X_PIXEL]; 
  166.     fwrite(t_plane, sizeof(char), X_PIXEL*Y_PIXEL, prn_stream);
  167.  
  168.  
  169.     gs_free((char *)out, line_size, 1, "cp50_print_page(out)");
  170.     gs_free((char *)r_plane, X_PIXEL*Y_PIXEL, 1, "cp50_print_page(r_plane)");
  171.     gs_free((char *)g_plane, X_PIXEL*Y_PIXEL, 1, "cp50_print_page(g_plane)");
  172.     gs_free((char *)b_plane, X_PIXEL*Y_PIXEL, 1, "cp50_print_page(b_plane)");
  173.     gs_free((char *)t_plane, X_PIXEL*Y_PIXEL, 1, "cp50_print_page(t_plane)");
  174.  
  175.     return 0;
  176. }
  177.  
  178. int private 
  179. cp50_output_page(gx_device *pdev, int num_copies, int flush)
  180. {   int code, outcode, closecode;
  181.  
  182.     code = gdev_prn_open_printer(pdev, 1);
  183.     if ( code < 0 ) return code;
  184.  
  185.     copies = num_copies; /* using global variable to pass */
  186.  
  187.     /* Print the accumulated page description. */
  188.     outcode = (*ppdev->printer_procs.print_page)(ppdev, ppdev->file);
  189.     if ( code < 0 ) return code;
  190.  
  191.     closecode = gdev_prn_close_printer(pdev);
  192.     if ( code < 0 ) return code;
  193.  
  194.     if ( ppdev->buffer_space ) /* reinitialize clist for writing */
  195.       code = (*gs_clist_device_procs.output_page)(pdev, num_copies, flush);
  196.  
  197.     if ( outcode < 0 ) return outcode;
  198.     if ( closecode < 0 ) return closecode;
  199.     if ( code < 0 ) return code;
  200.     return gx_finish_output_page(pdev, num_copies, flush);
  201. }
  202.  
  203.  
  204. /* 24-bit color mappers (taken from gdevmem2.c). */
  205. /* Note that Windows expects RGB values in the order B,G,R. */
  206.  
  207. /* Map a r-g-b color to a color index. */
  208. private gx_color_index
  209. cp50_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  210.   gx_color_value b)
  211. {   return ((ulong)gx_color_value_to_byte(r) << 16)+
  212.            ((uint)gx_color_value_to_byte(g) << 8) +
  213.            gx_color_value_to_byte(b);
  214. }
  215.  
  216. /* Map a color index to a r-g-b color. */
  217. private int
  218. cp50_color_rgb(gx_device *dev, gx_color_index color,
  219.   gx_color_value prgb[3])
  220. {   prgb[2] = gx_color_value_from_byte(color & 0xff);
  221.     prgb[1] = gx_color_value_from_byte((color >> 8) & 0xff);
  222.     prgb[0] = gx_color_value_from_byte(color >> 16);
  223.     return 0;
  224. }
  225.